#!/bin/bash

serviceName=yjtramdisk

function toUpperCase() {
        echo $@ | tr '[a-z]' '[A-Z]'
}

function toLowerCase() {
        echo $@ | tr '[A-Z]' '[a-z]'
}

# 询问参数的函数
function askParam() {
	paramName=$1
	defaultVal=$2
	paramDesc=$3
	echo -n "请为 $paramName 设定值"
	if [ "$paramDesc" != "" ]; then
		echo -n "（说明：$paramDesc）"
	fi
	if [ "$defaultVal" != "" ]; then
		echo -n "[$defaultVal]"
	fi
	echo "："
	read paramVal
	export PARAM_$paramName=$paramVal
}

if [ "$PARAM_ROLE" == "" ]; then
	askParam "ROLE" "" "设备角色，取值：ALL,CWS,CS,WS,IS,HSMD,DPS"
	PARAM_ROLE=$(toUpperCase $PARAM_ROLE)
fi
if [ "$PARAM_ROLE" == "" ]; then
	echo "ROLE is not set"
	exit
fi

sizeList=/aaa:1G,/bbb:4G,/ccc:1G
if [ "$PARAM_ROLE" == "IS" ]; then
	if [ "$PARAM_MEMORY" == "" ]; then
		askParam "MEMORY" "" "物理内存大小，取值：16G,32G,64G,128G"
		PARAM_MEMORY=$(toUpperCase $PARAM_MEMORY)
	fi
	if [ "$PARAM_MEMORY" == "" ]; then
		echo "MEMORY is not set"
		exit
	fi
	if [ "$PARAM_MEMORY" == "16G" ]; then
		sizeList=/aaa:1G,/bbb:2G,/ccc:500M
	elif [ "$PARAM_MEMORY" == "32G" ]; then
		sizeList=/aaa:1G,/bbb:2G,/ccc:500M
	elif [ "$PARAM_MEMORY" == "64G" ]; then
		sizeList=/aaa:1G,/bbb:5G,/ccc:500M
	elif [ "$PARAM_MEMORY" == "128G" ]; then
		sizeList=/aaa:1G,/bbb:5G,/ccc:500M
	else
		echo "MEMORY $PARAM_MEMORY is not support"
		exit
	fi
elif [ "$PARAM_ROLE" == "HSMD" ]; then
	if [ "$PARAM_MEMORY" == "" ]; then
		askParam "MEMORY" "" "物理内存大小，取值：8G,16G,32G"
		PARAM_MEMORY=$(toUpperCase $PARAM_MEMORY)
	fi
	if [ "$PARAM_MEMORY" == "" ]; then
		echo "MEMORY is not set"
		exit
	fi
	if [ "$PARAM_MEMORY" == "8G" ]; then
		sizeList=/ccc:2048M,/ddd:512M,/eee:1536M
	elif [ "$PARAM_MEMORY" == "16G" ]; then
		sizeList=/ccc:3072M,/ddd:1024M,/eee:2048M
	elif [ "$PARAM_MEMORY" == "32G" ]; then
		sizeList=/ccc:8G,/ddd:1G,/eee:3G
	else
		echo "MEMORY $PARAM_MEMORY is not support"
		exit
	fi
fi

if [ -f /etc/init.d/${serviceName} ]; then
	chkconfig --del ${serviceName}
fi

echo ">>>>Add Ramdisk Service"
echo "内存盘信息：${sizeList}"
if [[ "$PARAM_ROLE" == "HSMD" ]] || [[ "$PARAM_ROLE" == "ALL" ]]; then
	sed s#%SIZELIST%#${sizeList}# ./${serviceName}HSMD > /etc/init.d/${serviceName}
else
	sed s#%SIZELIST%#${sizeList}# ./${serviceName} > /etc/init.d/${serviceName}
fi

if [ "$PARAM_ROLE" == "HSMD" ]; then
	extServiceName=yjtftpuser
	cp -f ./$extServiceName /etc/init.d/
	chmod +x /etc/init.d/$extServiceName
fi

echo ">>>>Start Ramdisk Service"
chmod 755 /etc/init.d/${serviceName}
chkconfig --add ${serviceName}
service ${serviceName} start
